{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-07T10:45:44.019947Z",
     "iopub.status.busy": "2023-11-07T10:45:44.019757Z",
     "iopub.status.idle": "2023-11-07T10:45:44.801129Z",
     "shell.execute_reply": "2023-11-07T10:45:44.800531Z",
     "shell.execute_reply.started": "2023-11-07T10:45:44.019918Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "stage = 'A'\n",
    "\n",
    "df_train = pd.read_csv('../../../contest/train/GSLD_AGET_PAY.csv')\n",
    "df_test = pd.read_csv('../../../contest/A/GSLD_AGET_PAY_A.csv')\n",
    "\n",
    "save_path = '../data'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-07T10:45:44.802461Z",
     "iopub.status.busy": "2023-11-07T10:45:44.802258Z",
     "iopub.status.idle": "2023-11-07T10:45:44.851791Z",
     "shell.execute_reply": "2023-11-07T10:45:44.851235Z",
     "shell.execute_reply.started": "2023-11-07T10:45:44.802438Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "unit_typ_list = df_train.UNIT_TYP_CD.value_counts(ascending=False).index\n",
    "unit_typ_dic = {unit_typ_list[i]:i for i,_ in enumerate(unit_typ_list)}\n",
    "prov_cd_list = df_train.PROV_CD.value_counts(ascending=False).index\n",
    "prov_cd_dic = {prov_cd_list[i]:i for i,_ in enumerate(prov_cd_list)}\n",
    "# agen_cusno_list = df_train.AGEN_CUSNO.value_counts(False).index\n",
    "# agen_cusno_dic = {agen_cusno_list[i]:i for i,_ in enumerate(agen_cusno_list)}\n",
    "# agen_cusno_dic = df_train.AGEN_CUSNO.value_counts().to_dict() # 改为频次\n",
    "\n",
    "def prepro(df):\n",
    "    df.PROV_CD = df.PROV_CD.fillna(prov_cd_list[0]) # 用众数填充\n",
    "    df.PROV_CD = df.PROV_CD.map(prov_cd_dic)\n",
    "    df.UNIT_TYP_CD = df.UNIT_TYP_CD.fillna(unit_typ_list[0]) # 用众数填充\n",
    "    df.UNIT_TYP_CD = df.UNIT_TYP_CD.map(unit_typ_dic)\n",
    "    df.TR_AMT = df.TR_AMT.apply(lambda x: round(pow(x/3.12,3),2))\n",
    "#     df.AGEN_CUSNO = df.AGEN_CUSNO.map(agen_cusno_dic)\n",
    "    tmp_df = df.groupby('CUST_NO').agg(\n",
    "        cust_cnt = ('AGEN_CUSNO', 'count'),\n",
    "#         agen_custno_avg = ('AGEN_CUSNO', 'mean'), # 添加频次\n",
    "        tr_amt_sum = ('TR_AMT', 'sum'),\n",
    "        tr_amt_avg = ('TR_AMT', 'mean'),\n",
    "        prov_cd = ('PROV_CD', 'mean'),\n",
    "        unit_typ_cd = ('UNIT_TYP_CD', 'mean'),\n",
    "        tr_amt = ('TR_AMT', 'mean')\n",
    "    ).reset_index()\n",
    "    return tmp_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-07T10:45:44.852635Z",
     "iopub.status.busy": "2023-11-07T10:45:44.852458Z",
     "iopub.status.idle": "2023-11-07T10:45:45.451931Z",
     "shell.execute_reply": "2023-11-07T10:45:45.451352Z",
     "shell.execute_reply.started": "2023-11-07T10:45:44.852612Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "df_train = prepro(df_train)\n",
    "df_test = prepro(df_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-11-07T10:45:45.453042Z",
     "iopub.status.busy": "2023-11-07T10:45:45.452843Z",
     "iopub.status.idle": "2023-11-07T10:45:45.828039Z",
     "shell.execute_reply": "2023-11-07T10:45:45.827470Z",
     "shell.execute_reply.started": "2023-11-07T10:45:45.453009Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "df_train.to_csv(save_path+'/GSLD_AGET_PAY.csv', index=False)\n",
    "df_test.to_csv(save_path+'/GSLD_AGET_PAY_A.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
